package com.azzcs.blog.controller;

import com.azzcs.blog.entity.UserEntity;
import com.azzcs.blog.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;

/**
 * @Author: wzg
 * @Date: 2021/1/12 下午10:06
 */
@Controller
public class LoginController {

    @Autowired
    private UserService userService;

    @GetMapping("login")
    public String loginPage(Model model, HttpSession session){
        String sessionId = session.getId();
        model.addAttribute("port",port);
        model.addAttribute("sessionId",sessionId);
        return "login";
    }

    @Value("${server.port}")
    private String port;


    @PostMapping("/login/toLogin")
    public String login(Model model, HttpSession session, UserEntity userEntity){
        String sessionId = session.getId();
        boolean status = userService.login(sessionId,userEntity);
        model.addAttribute("port",port);
        model.addAttribute("sessionId",sessionId);
        if(status){
            return "redirect:/index";
        }else {
            return "login";
        }
    }

    @RequestMapping("/index")
    public String index(Model model, HttpSession session){
        String sessionId = session.getId();
        model.addAttribute("port",port);
        model.addAttribute("sessionId",sessionId);
        return "index";
    }
}
